import datetime
import uuid
from typing import List, Dict

from sqlalchemy import Column, Date, DateTime, Integer, String, Index, JSON, ForeignKey
from sqlalchemy.orm import Session, relationship

from common.database import Base
from common.database import SessionMixin


class MonitorLogCharacter(Base, SessionMixin):
    __tablename__ = 'monitor_log_character'
    service_name = Column(String(128), nullable=False, comment="服务名称")
    character_id = Column(String(32), default=uuid.uuid4().hex, nullable=False, comment="日志特征ID")
    content = Column(JSON, nullable=False, comment="日志内容")
    count = Column(Integer, nullable=False, comment="日志数量")
    monitor_log_service_uuid = Column(Integer, ForeignKey('monitor_log_service.uuid'), nullable=False, comment="监控日志服务uuid")
    # character2service = relationship('MonitorLogCharacter', backref='service2character')
    create_date = Column(Date, default=datetime.date.today, nullable=True, comment="创建日期")
    create_time = Column(DateTime, default=datetime.datetime.now, nullable=False, comment="创建时间")

    __table_args__ = (
        Index('idx_monitor_log_character_uuid', 'uuid', unique=True),
        Index('idx_monitor_log_character_datetime', 'create_time', unique=False),
        {
            'mysql_engine': 'innodb',
            'mysql_charset': 'utf8mb4',
            'mysql_collate': 'utf8mb4_unicode_ci',
            'extend_existing': True,
            'mysql_row_format': 'dynamic',
        }
    )

    def __repr__(self):
        return "<uuid='{}',character_id='{}',count='{}'>".format(self.uuid, self.character_id, self.count)

    # @classmethod
    # def get_by_service_name(cls, db: Session, service_name: str) -> Dict:
    #     return db.query(cls).filter(cls.service_name == service_name).to_dict_first()
    #
    # @classmethod
    # def get_policy_by_service_name(cls, db: Session, service_name: str) -> Dict:
    #     app = db.query(cls).filter(cls.service_name == service_name).first()
    #     return cls.to_dict(app.app2policy) if app else {}
    #
    # @classmethod
    # def get_entries(cls, db: Session) -> List[Dict]:
    #     return db.query(cls).outerjoin(
    #         MonitorLogPolicy, cls.monitor_log_policy_uuid == MonitorLogPolicy.uuid).with_entities(
    #         cls.uuid.label('uuid'), cls.service_name.label('service_name'), cls.app_log_status.label('service_log_status'),
    #         MonitorLogPolicy.exclude_keywords.label('exclude_keywords'),
    #         MonitorLogPolicy.alarm_threshold.label('alarm_threshold'),
    #         MonitorLogPolicy.is_maintain.label('is_maintain'),
    #         MonitorLogPolicy.alarm_plan.label('alarm_plan'),
    #         MonitorLogPolicy.maintain_begin_time.label('maintain_begin_time'),
    #         MonitorLogPolicy.maintain_end_time.label('maintain_end_time'),
    #         MonitorLogPolicy.notice_users.label('notice_users'),
    #         MonitorLogPolicy.create_time.label('create_time'),
    #         MonitorLogPolicy.update_time.label('update_time')).to_dict()
    #
    # @classmethod
    # def get_policy_paginate(cls, db: Session, page, per_page) -> List[Dict]:
    #     return db.query(cls).outerjoin(
    #         MonitorLogPolicy, cls.monitor_log_policy_uuid == MonitorLogPolicy.uuid).with_entities(
    #         cls.uuid.label('uuid'), cls.service_name.label('service_name'), cls.app_log_status.label('app_log_status'),
    #         MonitorLogPolicy.exclude_keywords.label('exclude_keywords'),
    #         MonitorLogPolicy.alarm_threshold.label('alarm_threshold'),
    #         MonitorLogPolicy.is_maintain.label('is_maintain'),
    #         MonitorLogPolicy.alarm_plan.label('alarm_plan'),
    #         MonitorLogPolicy.maintain_begin_time.label('maintain_begin_time'),
    #         MonitorLogPolicy.maintain_end_time.label('maintain_end_time'),
    #         MonitorLogPolicy.notice_users.label('notice_users'),
    #         MonitorLogPolicy.create_time.label('create_time'),
    #         MonitorLogPolicy.update_time.label('update_time')).paginate(page, per_page)
